﻿
var mongoose = require('mongoose');



exports.Insert = function (o) {







    var c = global.dbUzor.collection(o.collection);

    c.insertOne(o.data, function (a, b) {

        o.fun(a, b);

    });


}
exports.upAsync = function (o) {
    return new Promise((resolve, reject) => {


        if (o.multi == null) {
            o.multi = false
        }

        if (o.upsert == null) {
            o.upsert = false
        }

        var c = global.dbUzor.collection(o.collection);

        c.update(
            o.updateFind,
            o.updateSet, {
                multi: o.multi,
                upsert: o.upsert
            }, function (err, data) {
                console.log('开始执行update');
                if (err) {
                    reject(err)
                }
                resolve({
                    err: err,
                    data: data
                })
            }, {})


    });

};


exports.updateAsync = function (updateFind, updateSet, collection, dbName) {
    return new Promise((resolve, reject) => {




        var c = global.dbUzor.collection(collection);

        c.update(
            updateFind,
            updateSet, function (err, data) {
                console.log('开始执行update');

                resolve({
                    err: err,
                    data: data
                })
            }, {})


    });

};

exports.Save = function (o) {


    var iid = mongoose.Types.ObjectId(o.data._id);
    o.data._id = mongoose.Types.ObjectId(o.data._id);


    var c = global.dbUzor.collection(o.collection);




    c.save(o.data, function (a, b) {

        o.fun(a, b);

    });


}


exports.SaveAsync = function (o) {


    return new Promise(function (resolve, reject) {

        if (o.data._id) {

            o.data._id = mongoose.Types.ObjectId(o.data._id);
        }




        var c = global.dbUzor.collection(o.collection);




        c.save(o.data, function (a, b) {


            resolve({

                err: a,
                date: b
            })
        });
    });




}


exports.findOne = function (o) {



    var c = global.dbUzor.collection(o.collection);

    try {
        o.data._id = mongoose.Types.ObjectId(o.data._id);
    } catch (e) {
        console.log(' 尝试转换_id为objId失败');
    } finally {

        c.findOne(o.data, function (err, data) {




            o.fun(err, { info: data });

        });


    }
}



exports.countAsync = function (o) {
    var rt;
    return new Promise(function (resolve, reject) {


        var c = global.dbUzor.collection(o.collection);
        try {

            if (o.data._id) {
                o.data._id = mongoose.Types.ObjectId(o.data._id);

            }

        } catch (e) {
            console.log(' 尝试转换_id为objId失败');
        } finally {


            c.count(o.data, function (err, data) {

                //   db.close();

                if (err) {
                    reject(err)
                }
                else {
                    resolve(data);
                }

                //  o.fun(err, { info: data });

            });

        }


    })

}


exports.findOneAsync = function (o) {
    var rt;
    return new Promise(function (resolve, reject) {


        var c = global.dbUzor.collection(o.collection);
        try {

            if (o.data._id) {
                o.data._id = mongoose.Types.ObjectId(o.data._id);

            }

        } catch (e) {
            console.log(' 尝试转换_id为objId失败');
        } finally {


            c.findOne(o.data, function (err, data) {

                //   db.close();

                if (err) {
                    reject(err)
                }
                else {
                    resolve(data);
                }

                //  o.fun(err, { info: data });

            });

        }


    })

}


//查询多条数据
exports.findAsync = function (o) {



    return new Promise((resolve, reject) => {

        if (!o.data) {

            o.data = {};
        }

        if (!o.sort) {
            o.sort = {};
        }
        if (!o.limit) {

            o.limit = 1000;
        }
        if (!o.page) {
            o.page = 1;
        }

        if (!o.projection) {
            o.projection = {};
        }

        o.skip = (o.page - 1) * o.limit;
        var c = global.dbUzor.collection(o.collection);



        c.count(o.data, function myfunction(err, count) {


            var totalPage = Math.ceil(count / o.limit);


            c.find(o.data, o.projection).skip(o.skip).limit(o.limit).sort(o.sort).toArray(function (err, list) {
                if (err) {
                    reject(err)
                }
                else {
                    resolve({
                        list: list,
                        totalPage: totalPage, //总页码
                        total: count,  //条目总数
                        page: o.page, //当前页码
                        limit: o.limit  //一页多少条
                    })
                }




                //  db.close();
            });





        });


    });



    //  mongoose.connect('' + global.mgcon + '/' + o.db + '');








}

//查询多条数据
exports.find = function (o) {


    if (!o.data) {

        o.data = {};
    }

    if (!o.sort) {
        o.sort = {};
    }
    if (!o.limit) {

        o.limit = 1000;
    }
    if (!o.page) {
        o.page = 1;
    }

    o.skip = (o.page - 1) * o.limit;





    //  mongoose.connect('' + global.mgcon + '/' + o.db + '');

    var c = global.dbUzor.collection(o.collection);



    c.find(o.data).count(function myfunction(err, count) {


        var totalPage = Math.ceil(count / o.limit);


        c.find(o.data).skip(o.skip).limit(o.limit).sort(o.sort).toArray(function (err, list) {

            o.fun(err, {
                list: list,
                totalPage: totalPage, //总页码
                total: count,  //条目总数
                page: o.page, //当前页码
                limit: o.limit  //一页多少条

            });
            //  db.close();
        });





    });








}

//}
